package JDBCUtil;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;


import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
//jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false
public class InsertJDBC {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入ID：");
        int id = scanner.nextInt();
        System.out.println("请输入名字");
        String name = scanner.next();


        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("zkzsa123");

        Connection connection =  dataSource.getConnection();
        String sql = "insert into student values(?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);

        statement.setObject(1,id);
        statement.setObject(2,name);
        int n = statement.executeUpdate();
        System.out.println("n:"+n);
        statement.close();
        connection.close();
    }
}
